package sdk.webview.fmc.com.fmcsdk.check.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;


import sdk.webview.fmc.com.fmcsdk.R;

public class BottomPopupWindow {

    private PopupWindow popupWindow;
    private View contentView;

    public BottomPopupWindow(Context context, int layoutResId) {
        // 加载布局
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        contentView = inflater.inflate(layoutResId, null);
        View view_out = contentView.findViewById(R.id.view_out);
        if (view_out != null) {
            view_out.setOnClickListener(v -> {
                dismiss();
            });
        }

        // 初始化 PopupWindow
        popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setOutsideTouchable(true); // 点击外部区域关闭
        popupWindow.setFocusable(true); // 设置可获得焦点
        popupWindow.setBackgroundDrawable(null); // 设置背景透明（如果需要）
        WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.MATCH_PARENT;
        popupWindow.setWidth(params.width);
        popupWindow.setHeight(params.height);
        popupWindow.setClippingEnabled(false); // 允许扩展到屏幕外
        // 设置动画（可选）
        popupWindow.setAnimationStyle(R.style.BottomPopupWindowAnimation);

    }
    public BottomPopupWindow(Context context, int layoutResId,boolean isOut) {
        // 加载布局
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        contentView = inflater.inflate(layoutResId, null);
        // 初始化 PopupWindow
        popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setOutsideTouchable(true); // 点击外部区域关闭
        popupWindow.setFocusable(true); // 设置可获得焦点
        popupWindow.setBackgroundDrawable(null); // 设置背景透明（如果需要）
        WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.MATCH_PARENT;
        popupWindow.setWidth(params.width);
        popupWindow.setHeight(params.height);
        popupWindow.setClippingEnabled(false); // 允许扩展到屏幕外
        // 设置动画（可选）
        popupWindow.setAnimationStyle(R.style.BottomPopupWindowAnimation);
    }

    /**
     * 显示 PopupWindow 在底部
     *
     * @param anchor 依附的视图
     */
    public void show(View anchor) {
        if (popupWindow != null && !popupWindow.isShowing()) {
            // addDimBackground(anchor);
            popupWindow.showAtLocation(anchor, Gravity.BOTTOM, 0, 0);
        }
    }



    private View anchor;
    private View dimView; // 用于显示灰色半透明背景

    private void addDimBackground(View anchor) {
        this.anchor = anchor;
        if (dimView == null) {
            dimView = new View(anchor.getContext());
            dimView.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
            ));
            dimView.setBackground(anchor.getResources().getDrawable(R.drawable.shape_800000));
        }

        ((ViewGroup) anchor.getRootView()).addView(dimView);
    }

    /**
     * 隐藏 PopupWindow
     */
    public void dismiss() {
        if (popupWindow != null && popupWindow.isShowing()) {
            // removeDimBackground();
            popupWindow.dismiss();
        }
    }

    /**
     * 移除灰色半透明背景
     */
    private void removeDimBackground() {
        if (anchor != null && dimView != null && dimView.getParent() != null) {
            ((ViewGroup) anchor.getRootView()).removeAllViews();
        }
    }

    /**
     * 设置点击事件监听器
     *
     * @param onClickListener 点击事件监听器
     */
    public void setOnClickListener(View.OnClickListener onClickListener) {
        contentView.setOnClickListener(onClickListener);
    }

    /**
     * 设置触摸事件监听器
     *
     * @param onTouchListener 触摸事件监听器
     */
//    public void setOnTouchListener(View.OnTouchListener onTouchListener) {
//        popupWindow.setOnTouchListener(onTouchListener);
//    }

    /**
     * 设置弹出窗口消失时的监听器
     *
     * @param onDismissListener 消失时的监听器
     */
    public void setOnDismissListener(PopupWindow.OnDismissListener onDismissListener) {
        popupWindow.setOnDismissListener(onDismissListener);
    }

    /**
     * 获取内容视图
     *
     * @return 内容视图
     */
    public View getContentView() {
        return contentView;
    }
}
